package com.sunyur.tailor.channel.service.inner.expense.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.expense.param.ExpenseTypeListParam;
import com.sunyur.tailor.channel.service.inner.expense.param.ExpenseTypeStatusListParam;
import com.sunyur.tailor.channel.service.inner.expense.service.SunyurExpenseTypeService;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:费用类型服务实现
 * @author: pengbing
 * @create: 2021-06-01 17:20
 **/
@Service("SunyurExpenseService")
public class SunyurExpenseTypeServiceImpl implements SunyurExpenseTypeService {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurExpenseTypeServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurExpenseTypeServiceImpl.class);
    private static final int MAX_SIZE = 20;
    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    @Value("${sunyur.api.batchSyncExpenseType}")
    private String batchSyncExpenseType;

    @Value("${sunyur.api.batchSyncExpenseTypeStatus}")
    private String batchSyncExpenseTypeStatus;

    @Override
    public ChannelResult<Boolean> batchSyncExpenseType(ExpenseTypeListParam expenseTypeListParam) throws Exception {
        LOGGER_INFO.info("SunyurExpenseServiceImpl batchSyncExpenseType param is {}", JSON.toJSONString(expenseTypeListParam));
        if (ObjectUtils.isEmpty(expenseTypeListParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        //根据长度分割
        List<List<ExpenseTypeListParam.ExpenseTypeParam>> partition = Lists.partition(expenseTypeListParam.getExpenseTypes(), MAX_SIZE);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        for (List<ExpenseTypeListParam.ExpenseTypeParam> expenseTypesParams : partition) {
            // 调用主线接口
            expenseTypeListParam.setExpenseTypes(expenseTypesParams);
            SunyurResult sunyurResult = sunyurClient.execPost(batchSyncExpenseType, JSON.toJSONString(expenseTypeListParam), null);
            if (!"success".equals(sunyurResult.getCode())) {
                LOGGER_ERROR.error("batchSyncExpenseType loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(expenseTypesParams), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (CollectionUtils.isNotEmpty(errorMsgList)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(Boolean.TRUE);
    }

    @Override
    public ChannelResult<Boolean> batchSyncExpenseTypeStatus(ExpenseTypeStatusListParam expenseTypeStatusListParam) throws Exception {
        LOGGER_INFO.info("SunyurExpenseServiceImpl batchSyncExpenseTypeStatus param is {}", JSON.toJSONString(expenseTypeStatusListParam));
        if (ObjectUtils.isEmpty(expenseTypeStatusListParam)) {
            return ChannelResultUtil.error(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        }
        //根据长度分割
        List<List<String>> partition = Lists.partition(expenseTypeStatusListParam.getExpenseTypeCodes(), MAX_SIZE);
        // 错误信息List
        List<String> errorMsgList = Lists.newArrayList();
        for (List<String> strings : partition) {
            // 调用主线接口
            expenseTypeStatusListParam.setExpenseTypeCodes(strings);
            SunyurResult sunyurResult = sunyurClient.execPost(batchSyncExpenseTypeStatus, JSON.toJSONString(expenseTypeStatusListParam), null);
            if (!"success".equals(sunyurResult.getCode())) {
                LOGGER_ERROR.error("batchSyncExpenseTypeStatus loop call sunyur request fail, param is {}, result is {}", JSON.toJSONString(strings), JSON.toJSONString(sunyurResult));
                // 每批次错误信息收集
                errorMsgList.add(sunyurResult.getContent() + "-" + sunyurResult.getErrorMessage());
            }
        }
        // 存在错误信息
        if (!CollectionUtils.isEmpty(errorMsgList)) {
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), JSON.toJSONString(errorMsgList));
        }
        return ChannelResultUtil.success(Boolean.TRUE);
    }
}
